package com.example.movie.common.dao

import androidx.room.*
import com.example.movie.common.entity.Agent
import com.example.movie.common.entity.Plan

@Dao
interface CinemaDao {

    @Query("SELECT * FROM  `Plan`")
    fun getAll(): List<Plan>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(plans: List<Plan>?)

    @Delete
    fun delete(plan: Plan)

    @Query("SELECT * FROM `Plan` WHERE movieId= :movieId AND playDay= :playDay")
    fun loadByMovieIdAndPlayDay(movieId: Int, playDay: String): List<Plan>

    /**
     * 查询评分最高的电影排片记录
     */
    @Query("SELECT * FROM `Plan` p WHERE p.score=(SELECT MAX(score) FROM `Plan`)")
    fun highScoreMoviePlan(): List<Plan>

    /**
     * 查询排片电影简单信息列表(movieId,movieImg)
     */
    @Query("SELECT DISTINCT p1.movieId,p1.movieImg FROM `Plan`p1 INNER JOIN `Plan` p2 on p1.movieId=p2.movieId")
    fun planMovieImages(): List<SimpleMovieData>

    /**
     * 查询影片对应的拍片日期列表
     */
    @Query("SELECT DISTINCT playDay FROM `plan` WHERE movieId=:movieId")
    fun planDates(movieId: Int): List<String>

    /**
     * 清空排片计划表
     */
    @Query("DELETE FROM `Plan`")
    fun deleteAllPlanData(): Int

    @Query("DELETE FROM AGENT")
    fun deleteAllAgents():Int

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAllAgents(plans: List<Agent>?)

    @Query("SELECT * FROM Agent WHERE CASE WHEN :star!=0 THEN star=:star ELSE 1=1 END ORDER BY CASE WHEN :price=0 THEN 1=1 END, CASE WHEN :price=1 THEN lowestPrice END ASC ,CASE WHEN :price=2 THEN lowestPrice END DESC")
    fun getAgentCondition(price: Int, star: Int): List<Agent>

}

data class SimpleMovieData (

    val movieId:Int?,
    val movieImg:String?

)
    